package com.rytong.tools.datastorage;

import android.app.Activity;

//数据库相关
public class PackageManagerAndroid {
    /** Define the data type need to be accessed through this class */
    final static int COOKIE = 1;

    public final static int CHANNELS_HISTORY_SIZE = 5;

    /** Keys to classify information in the RMS store. */
    public final static String RMSKEY_COOKIE = "cookie";
    public final static String RMSKEY_LOGINKEY = "loginkey";
    public final static String RMSKEY_USERID = "userid";
    public final static String RMSKEY_LAUNCH_SCREEN = "isShowLS";
    public final static String RMSKEY_CHANNEL_DEFAULT = "chDefShows";
    public final static String RMSKEY_ZOOM_LEVEL = "defZoomL";
    public final static String RMSKEY_FIRST_LOGIN = "isFirstLogin";
    public final static String RMSKEY_GATEWAY = "gateway";
    public final static String RMSKEY_LOCATION = "initlocation";
    public final static String RMSKEY_POI = "initpoi";
    public final static String RMSKEY_CHANNELID = "initchannel";
    public final static String RMSKEY_CHANNEL_USERNAME = "channelUser";// for
    // channel
    // action use.
    public final static String RMSKEY_CHANNEL_PASSWORD = "channelPassword";
    public final static String RMSKEY_CHANNELSORT = "chsort";
    public final static String RMSKEY_LOCATION_HISTORY = "location_history";
    public final static String RMSKEY_CURRENTMAP = "map"; // Stores current map
    // ID and
    // name.
    public final static String RMSKEY_CHANNEL_APP = "app_channels";
    public final static String RMSKEY_MESSAGE_LIST = "messages_list";
    public final static String RMSKEY_CHANNEL_REALID = "channeRealId";
    public final static String RMSKEY_LOGODATA = "gogo_data";
    public final static String RMSKEY_QUERY_STRING = "queryString";// current
    // query.
    // final static String RMSKEY_ACCESS_TYPE = "accessPoint"; // cmwap or cmnet

    public final static String PERM_DB = "LightPole_DB"; // This is historical.
    public final static String MSGSUM_DB = "LP_MESSAGE_LIST";
    public final static String SESSION_DB = "LP_SESSION";
    public final static String POI_DB = "LP_POI";
    public final static String MAP_DB = "LP_MAP_DB";
    public final static String CHANNEL_HISTORY_DB = "LP_CHANNEL_HISTORY";
    public final static String LOGIN_DB = "LP_LOGIN_DB";
    public final static String USERCODE_DB = "UserCode_DB";
    public final static String BANKUSERCODE_DB = "BankUserCode_DB";
    public final static String TMSUSERCODE_DB = "TmsUserCode_DB";
    public final static String AUTOTEXT_DB = "AutoText_DB";
    public final static String SAVEPROPERTY_DB = "SAVEPROPERTY_DB";
    public final static String ANDROID_DB = "ANDROID_DB";
    public final static String ISFIRST_DB = "ISFIRST_DB";
    // 控件存储属性集合的存储对象
    public final PreferenceDB SAVEPROPERTYDB_;
    /** Stores permanent user information invariant per session */
    public final PreferenceDB permDB_;
    // 存储手机的常用信息
    public static PreferenceDB ANDROIDDB;
    /** Stores Message List infor. */
    public final PreferenceDB messageSummaryDB_;

    /** Stores per-session data, such as channel info. */
    public final PreferenceDB sessionDB_;

    /** Stores POI information */
    public final PreferenceDB poiDB_;

    /** Stores map tiles */
    // final MapDB mapDB_;

    /**
     * Stores history of channels
     */

    public final PreferenceDB userCodeDB_;
    // 自动联想控件存储对象
    public final PreferenceDB autoTextDB_;
    public static PreferenceDB LUADB;
    public final PreferenceDB sqlSizeDB_;
    private String cookie_ = null;
    private boolean loggedIn_ = false;

    public boolean isInitStorage_ = false;

    public final String LUA_DB = "LUA_DB";

    private String SQLSIZE_DB = "SQLSIZE_DB";

    public PackageManagerAndroid(Activity app) throws Exception {
        permDB_ = new PreferenceDB(app, PERM_DB);
        messageSummaryDB_ = new PreferenceDB(app, MSGSUM_DB);
        sessionDB_ = new PreferenceDB(app, SESSION_DB);
        poiDB_ = new PreferenceDB(app, POI_DB);
        // mapDB_ = new MapDB(app, MAP_DB);
        userCodeDB_ = new PreferenceDB(app, USERCODE_DB);
        autoTextDB_ = new PreferenceDB(app, AUTOTEXT_DB);
        SAVEPROPERTYDB_ = new PreferenceDB(app, SAVEPROPERTY_DB);
        LUADB = new PreferenceDB(app, LUA_DB);
        sqlSizeDB_ = new PreferenceDB(app, SQLSIZE_DB);
        ANDROIDDB = new PreferenceDB(app, ANDROID_DB);
    }

    /**
     * Generic method to read a string from preference database given a key.
     */
    public String readString(PreferenceDB prefDB, String key) throws Exception {

        // why is the method 'open' here?
        // prefDB.open();
        try {
            return prefDB.readAndDestroy(key);
        } finally {
            // prefDB.close();
        }
    }

    /**
     * Separately initialize RMS outside constructor, so that we can put these in a separate thread.
     */
    public void initStorage() throws Exception {
        isInitStorage_ = true;

        // permDB_.init();
        messageSummaryDB_.init();
        sessionDB_.init();
        poiDB_.init();
        // mapDB_.init(); // Must be done after preference DB is initialized.
    }

    /**
     * Generic method to save a string to preference database.
     */
    public void storeString(PreferenceDB prefDB, String key, String str) throws Exception {
        if (str != null) {

            // why is the method 'open' here?
            // prefDB.open();
            try {
                prefDB.write(key, str);
            } finally {
                // prefDB.close();
            }
        }
    }
}
